package rcs34.android.photopublishare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	private final String LOGTAG = getClass().getName();
	private static final int VERSION = 6;
	public static final String TABLE_TARGETS = "targets";
	public static final String COL_ROWID = "rowid";
	public static final String COL_NAME = "name";
	public static final String COL_URL = "url";
	public static final String COL_LOGIN = "login";
	public static final String COL_PASSWORD = "password";
	public static final String COL_PORT = "port";
	public static final String COL_SELECTED = "selected";
	public static final String TABLE_TARGETS_STRUCT = " (" + COL_NAME
			+ " VARCHAR(50) NOT NULL, " + COL_URL + " VARCHAR(255) NOT NULL, "
			+ "" + COL_LOGIN + " VARCHAR(50), " + COL_PASSWORD
			+ " VARCHAR(50)," + COL_PORT + " INTEGER, " + COL_SELECTED
			+ " BOOLEAN) ";
	private static String[] args = new String[1];
	private static final String[] columns = new String[] { "the_name" };

	public DBHelper() {
		super(GApp.context, DBHelper.class.getPackage() + ".db", null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase _db) {
		createStuff(_db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		createStuff(db);
	}

	private void createStuff(SQLiteDatabase db) {
		try {
			String query = "DROP TABLE IF EXISTS " + TABLE_TARGETS;
			db.execSQL(query);
			query = "CREATE TABLE " + TABLE_TARGETS + TABLE_TARGETS_STRUCT;
			db.execSQL(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		// hack
		InputStream is = GApp.context.getResources().openRawResource(
				R.raw.settings);
		String line;
		try {
			BufferedReader r = new BufferedReader(new InputStreamReader(is));
			while ((line = r.readLine()) != null) {
				line = line.trim();
				//Log.v(LOGTAG, "line "+line);
				String[] aLine = line.split(",");
				ContentValues cv = new ContentValues();
				cv.clear();
				cv.put(COL_NAME, aLine[0]);
				cv.put(COL_URL, aLine[1]);
				cv.put(COL_LOGIN, aLine[2]);
				cv.put(COL_PASSWORD, aLine[3]);
				cv.put(COL_PORT, aLine[4]);
				db.insertOrThrow(TABLE_TARGETS, null, cv);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
	}

	public void updateAll(ArrayList<ContentValues> _records) {
		SQLiteDatabase db = getWritableDatabase();
		for (ContentValues record : _records) {
			if (record.getAsLong(COL_ROWID) != null) {
				db.replace(TABLE_TARGETS, null, record);
			} else {
				db.insert(TABLE_TARGETS, null, record);
			}
		}
		db.close();
	}

	public void updateRecord(ContentValues _record) {
		SQLiteDatabase db = getWritableDatabase();
		Log.v(LOGTAG, _record.getAsString(COL_ROWID) + "");
		if (_record.getAsLong(COL_ROWID) != null) {
			db.replace(TABLE_TARGETS, null, _record);
		} else {
			db.insert(TABLE_TARGETS, null, _record);
		}
		db.close();
		// Log.v(LOGTAG,"Saved");
	}

	public void deleteRecord(String _rowid) {
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TABLE_TARGETS, COL_ROWID + "='" + _rowid + "'", null);
		db.close();
		// Log.v(LOGTAG,"Deleted");
	}

	public ArrayList<ContentValues> getAllTargets() {
		ArrayList<ContentValues> result = new ArrayList<ContentValues>();
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(TABLE_TARGETS,
				new String[] { COL_ROWID, "*" }, null, null, null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				ContentValues record = new ContentValues();
				record.put(COL_NAME,
						cursor.getString(cursor.getColumnIndex(COL_NAME)));
				record.put(COL_URL,
						cursor.getString(cursor.getColumnIndex(COL_URL)));
				record.put(COL_LOGIN,
						cursor.getString(cursor.getColumnIndex(COL_LOGIN)));
				record.put(COL_PASSWORD,
						cursor.getString(cursor.getColumnIndex(COL_PASSWORD)));
				record.put(COL_PORT,
						cursor.getInt(cursor.getColumnIndex(COL_PORT)));
				record.put(COL_ROWID,
						cursor.getInt(cursor.getColumnIndex(COL_ROWID)));
				record.put(COL_SELECTED,
						cursor.getInt(cursor.getColumnIndex(COL_SELECTED)));
				result.add(record);
				cursor.moveToNext();
			}
		}
		Log.v(LOGTAG, "Closing");
		cursor.close();
		db.close();
		return result;
	}

	public ContentValues getTarget(String _rowId) {
		ContentValues record = null;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(TABLE_TARGETS,
				new String[] { COL_ROWID, "*" }, COL_ROWID + "='" + _rowId
						+ "'", null, null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			record = new ContentValues();
			record.put(COL_NAME,
					cursor.getString(cursor.getColumnIndex(COL_NAME)));
			record.put(COL_URL,
					cursor.getString(cursor.getColumnIndex(COL_URL)));
			record.put(COL_LOGIN,
					cursor.getString(cursor.getColumnIndex(COL_LOGIN)));
			record.put(COL_PASSWORD,
					cursor.getString(cursor.getColumnIndex(COL_PASSWORD)));
			record.put(COL_PORT, cursor.getInt(cursor.getColumnIndex(COL_PORT)));
			record.put(COL_ROWID,
					cursor.getInt(cursor.getColumnIndex(COL_ROWID)));
			record.put(COL_SELECTED,
					cursor.getInt(cursor.getColumnIndex(COL_SELECTED)));
		}
		Log.v(LOGTAG, "Closing");
		cursor.close();
		db.close();
		return record;
	}
}
